
// 新增页面表单编辑后事件

import afterEditForm from '../request/afterEditForm';
import { HI_PSNJOB_H_DEFS } from '../../../request-functions/constant';

export default async function (props, moduleId, key, value, oldValue, func_type = '1') {
    const {
        humanResourceOrganizationValue: { refpk },
        form: {
            getAllFormValue,
            setAllFormValue,
            setFormItemsDisabled,
            getFormItemsValue,
            setFormItemsValue,
            setFormItemsVisible,
            setFormItemsRequired,
        },
        cardTable: { setValByKeyAndIndex, getAllRows },
    } = this.props;

    if (moduleId === "hi_psnjob_h" && key === "begindate") {
        let psnjobData = getAllRows("hi_psnjob");
        if (psnjobData.length === 1 && !psnjobData[0].values.begindate.value) {
            setValByKeyAndIndex("hi_psnjob", 0, "begindate", value);
        }
    }
    // 勾选了是否签署竞业协议
    if (moduleId === "hi_psnjob_h" && key === "jobglbdef34") {
        setFormItemsVisible(moduleId, {
            [HI_PSNJOB_H_DEFS.JYXZBCJZFBZ]: value.value,
        });
        setFormItemsRequired(moduleId, {
            [HI_PSNJOB_H_DEFS.JYXZBCJZFBZ]: value.value,
        });
    }

    // 人员类别选择实习生
    if (moduleId === "hi_psnjob_h" && key === "pk_psncl") {
        let psnclFlag = false;
        if (value && value.display == "实习生") {
            psnclFlag = true;
        }
        setFormItemsVisible(moduleId, {
            [HI_PSNJOB_H_DEFS.SXSZDLS]: psnclFlag,
            [HI_PSNJOB_H_DEFS.SXSBXFJE]: psnclFlag,
            [HI_PSNJOB_H_DEFS.SXSJT]: psnclFlag,
        });

        setFormItemsRequired(moduleId, {
            [HI_PSNJOB_H_DEFS.SXSZDLS]: psnclFlag,
            [HI_PSNJOB_H_DEFS.SXSBXFJE]: psnclFlag,
            [HI_PSNJOB_H_DEFS.SXSJT]: psnclFlag,
        });
    }

    // 人员类别选择退休返聘
    if (moduleId === "hi_psnjob_h" && key === "pk_psncl") {
        let psnclFlag = false;
        if (value && value.display == "返聘人员") {
            psnclFlag = true;
        }
        setFormItemsVisible(moduleId, {
            [HI_PSNJOB_H_DEFS.TXFPLWBC]: psnclFlag
        });
        setFormItemsRequired(moduleId, {
            [HI_PSNJOB_H_DEFS.TXFPLWBC]: psnclFlag
        });
    }

    // 勾选了是否职业病危害岗位
    if (moduleId === "hi_psnjob_h" && key === HI_PSNJOB_H_DEFS.SFZYBWHGW && value.value) {
        // 弹出职业危害调研页面
        this.props.updateState({
            zywhdyModalVisible: true
        });
    }
    
    // 是否取得原单位离职证明
    if (moduleId === "hi_psnjob_h" && key === HI_PSNJOB_H_DEFS.SFQDYDWLZZM) {
        let flag = true;
        if (value.value) {
            flag = false;
        }
        setFormItemsVisible(moduleId, {
            [HI_PSNJOB_H_DEFS.WQDYDWLZZMYY]: flag
        });
        setFormItemsRequired(moduleId, {
            [HI_PSNJOB_H_DEFS.WQDYDWLZZMYY]: flag
        });
    }
    

    let postData = {
        areaCode: moduleId,
        func_type: func_type,
        key: key,
        model: getAllFormValue(moduleId),
        pk_org: refpk,
    };

    try {
        let res = await afterEditForm(postData);
        if (res.data.disableItem) {
            Object.keys(res.data.disableItem).forEach((key) => {
                setFormItemsDisabled(moduleId, {
                    [key]: res.data.disableItem[key],
                });
            });
        }
        let formData = res.data && res.data.formData[moduleId];

        setAllFormValue({
            [moduleId]: formData,
        });
        if (moduleId === "bd_psndoc" && key === "code") {
            let val = getFormItemsValue("bd_psndoc", "code");
            setFormItemsValue("hi_psnjob_h", { clerkcode: val });
        }
    } catch (e) {}
}
